// 引入创建pinia对象的函数
import{defineStore}from 'pinia'
import type { CategoryStore } from '@/api/product/model/categoryModel'

// 引入发送请求函数
import {reqGetCategory1ListData,reqGetCategory2ListData,reqGetCategory3ListData} from '@/api/product/category'


//创建实列对象
 export const useCategoryListStore  = defineStore('CategoryList',{
  state:():CategoryStore=>{
    return {

      //存储三级导航每个选项框对应数据的id
      category1Id:undefined,
      category2Id:undefined,
      category3Id:undefined,
      
      // 存储每个下拉框中的数据
      category1List:[],
      category2List:[],
      category3List:[],

    }
  },
  actions:{
   // 发送请求获取一级导航数据
   async getCategory1ListData(){
    // 获取请求返回值 一级导航列表
      const result = await reqGetCategory1ListData();
      this.category1List = result;
   },
  // 发送请求获取二级导航数据 (也就是一级导航id发生改变之时)
  async getCategory2ListData(id:number){
     this.category1Id = id
    //要隐藏二级导航和三级导航的id搜索框不能有内容
     this.category2Id = undefined
     this.category3Id = undefined
     this.category3List = [] // 将三级列表也要清空
     // 获取请求返回值
     const result = await reqGetCategory2ListData(id);
      this.category2List = result;
  },
  // 发送请求获取三级级导航数据
  async getCategory3ListData(id:number){
    this.category2Id = id

      // 将三级数据重置
      this.category3Id = undefined
    // 获取请求返回值
    const result = await reqGetCategory3ListData(id);
     this.category3List = result;
 },
   //  传送三级导航id
    saveCategory3Id(id: number){
      this.category3Id = id
    }
  },
 
})


